//
//  ViewController.m
//  任务列表练习
//
//  Created by apple on 15/9/15.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "ValueTableViewCell.h"
#import "SecondViewController.h"
#import "Masonry.h"
#import "ThreeViewController.h"
#import "DateManager.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

{
    NSIndexPath *_deletaIndexPath;
    
//    NSMutableArray *_reArray;
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //导航条右图标
    _reArray = [[NSMutableArray alloc] initWithContentsOfFile:@"/Users/apple/Desktop/123.text"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Enter"] style:UIBarButtonItemStylePlain target:self action:@selector(EnterClick:)];
    
    self.title = @"任务列表";
    
    CGSize size = self.view.bounds.size;
    
    _tableView = [[UITableView alloc] init];
    
    //代理
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    //注册cell
    
    [_tableView registerClass:[ValueTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //    显示tableView
    
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(@(10));
         make.left.equalTo(@(0));
         make.size.equalTo(MASBoxValue(size));
     }];
    
    
    
}

//Source代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _reArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ValueTableViewCell *cell = (ValueTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.row % 2)
    {
        cell.colorView.backgroundColor = [UIColor yellowColor];
    }
    else
    {
        cell.colorView.backgroundColor = [UIColor greenColor];
    }
    
    cell.imageView.image = [UIImage imageNamed:@"123"];
    
    
    NSMutableDictionary *dict = _reArray[indexPath.row];
    
    
    cell.textLabel.text = dict[@"title"];
    
    NSLog(@"%@",cell.textLabel.text);
    
    cell.detailTextLabel.text = @"小鸡炖蘑菇";
    
    //按键的功能和实现的方式
    
    
    //按键的标题
    [cell.enterButton setTitle:@">" forState:UIControlStateNormal];
    
    [cell.enterButton addTarget:self action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    cell.textLabel.text = [DateManager new].title;
    
//    [tableView reloadData];
    return cell;
}

- (void)EnterClick:(UIButton *)sender
{
    SecondViewController *secondView = [[SecondViewController alloc] init];
    
    
    //自动隐藏TabBar（标签栏）
    
    secondView.hidesBottomBarWhenPushed = YES;
    secondView.viewCtrl = self;
    
    [self.navigationController pushViewController:secondView animated:YES];
    
}
//- (void)viewWillAppear:(BOOL)animated {
//    _reArray = [[NSMutableArray alloc] initWithContentsOfFile:@"/Users/apple/Desktop/123.text"];
//    [_tableView reloadData];
//}

//删除内容提醒－－－UIAlerView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        _deletaIndexPath = indexPath;
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"您确定要删除选中的这条内容嘛？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        //弹出提示框
        
        [alertView show];
        
    }
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 1)
    {
        [ _reArray removeObjectAtIndex:_deletaIndexPath.row];
        
        [_tableView deleteRowsAtIndexPaths:@[_deletaIndexPath] withRowAnimation:
         UITableViewRowAnimationAutomatic];
        
        _deletaIndexPath = nil;
    }
}


//－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－

//获取用户选择，当用户点击时，阴影会一闪而过；

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－


- (void)didClick:(UIButton *)sender
{
    
    ThreeViewController *threeViewCtrl = [[ThreeViewController alloc] init];
    
    [self.navigationController pushViewController:threeViewCtrl animated:YES];
    
//    [self.navigationController setViewControllers:nArray animated:YES];

}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [_tableView reloadData];
//}
@end
